import { Body, Inject, Post, Provide } from '@midwayjs/decorator'
import { InjectEntityModel } from '@midwayjs/typeorm'
import { BaseController, CoolController, CoolUrlTag, TagTypes } from '@cool-midway/core'
import type { Repository } from 'typeorm'
import { NewsInfoEntity } from '../../entity/info'
import type { NewsInfoService } from '../../service/info'

/**
 * 字典信息
 */
@Provide()
@CoolController({
  api: ['list', 'page'],
  entity: NewsInfoEntity,
  listQueryOp: {
    keyWordLikeFields: ['title'],
    fieldEq: ['status'],
    where: async () => {
      return [
        ['a.status = :status', { status: 1 }],
      ]
    },
  },
  pageQueryOp: {
    keyWordLikeFields: ['title'],
    fieldEq: ['status'],
    where: async () => {
      return [
        ['a.status = :status', { status: 1 }],
      ]
    },
  },
})
@CoolUrlTag({
  key: TagTypes.IGNORE_TOKEN,
  value: ['detail', 'list', 'page'],
})
export class AppNewsInfoController extends BaseController {
  @InjectEntityModel(NewsInfoEntity)
  newsInfoEntity: Repository<NewsInfoEntity>

  @Inject()
  newsInfoService: NewsInfoService

  /**
   * 新闻详情
   */
  @Post('/detail', { summary: '新闻详情' })
  async detail(@Body() query) {
    return this.ok(await this.newsInfoService.detail(query))
  }
}
